import re
import os
import sys
from collections import defaultdict
import pprint

def process_log(log_content):
    pattern = r'\[B\((\d+),(\d+),(\d+)\)T\((\d+),(\d+),(\d+)\)\]'

    block_thread_id_ranges = []
    for m in re.finditer(pattern, log_content):
        # ① 起始位置
        start_pos = m.start()          # 整条匹配的开始下标
        end_pos = m.end()
        
        # ② 提取 6 个数字
        bx, by, bz, tx, ty, tz = map(int, m.groups())
        prefix_range = (start_pos, end_pos)
        block_thread_id = (bx, by, bz, tx, ty, tz)
        block_thread_id_ranges.append((prefix_range, block_thread_id))
    
    block_thread_id_logs = defaultdict(str)
    for i in range(len(block_thread_id_ranges)):
        block_thread_id = block_thread_id_ranges[i][1]
        end_pos = block_thread_id_ranges[i][0][1]
        if i != len(block_thread_id_ranges) - 1:
            next_start_pos = block_thread_id_ranges[i + 1][0][0]
        else:
            next_start_pos = len(log_content)
        block_thread_id_logs[block_thread_id] += log_content[end_pos: next_start_pos]

    pprint.pp(block_thread_id_logs)

    for block_thread_id, log in block_thread_id_logs.items():
        bx, by, bz, tx, ty, tz = block_thread_id
        key = f"B_{bx}_{by}_{bz}_T_{tx}_{ty}_{tz}"
        filename = f"{key}.log"
        with open(filename, 'w') as f:
            f.write(log)
    
if __name__ == "__main__":
    if len(sys.argv) > 1:
        # 从文件读取日志内容
        log_file = sys.argv[1]
        f = open(log_file, 'r')
    else:
        f = sys.stdin
    log_content = f.read()
    f.close()
    process_log(log_content)